package com.mycat.redis.pool;

import redis.clients.jedis.JedisPoolConfig;

import java.util.ResourceBundle;

/**
 * 获取Redis的相关配置
 *
 * @author roger.
 * @email 190642964@qq.com
 * @create 2016-12-21
 */
public class RedisConfigHelper {
    /**
     * 本地redis的配置文件地址
     */
    private static final String localBundleLocal = "config.redis";
    /**
     * 系统redis的配置文件地址
     */
    private static final String systemBundleLocal = "config.redis";
    /**
     * 获取配置文件
     */
    private static ResourceBundle bundle;
    /**
     * Redis相关配置
     */
    private static JedisPoolConfig jedisPoolConfig;
    /**
     * redis权限访问密码
     */
    private static String host;
    /**
     * 超时时间
     */
    private static Integer port;

    /**
     * redis权限访问密码
     */
    private static String password;
    /**
     * 超时时间
     */
    private static Integer timeout;

    private RedisConfigHelper() {
    }

    /**
     * 获取JedisPoolConfig
     *
     * @return JedisPoolConfig
     */
    public static JedisPoolConfig getJedisPoolConfig() {
        if (null == jedisPoolConfig) {
            jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setTestWhileIdle(Boolean.valueOf(getResourceBundle().getString("testWhileIdle")));
            jedisPoolConfig.setMinEvictableIdleTimeMillis(Long.valueOf(getResourceBundle().getString("minEvictableIdleTimeMillis")));
            jedisPoolConfig.setNumTestsPerEvictionRun(Integer.valueOf(getResourceBundle().getString("numTestsPerEvictionRun")));
            jedisPoolConfig.setMaxTotal(Integer.valueOf(getResourceBundle().getString("maxTotal")));
            jedisPoolConfig.setMaxIdle(Integer.valueOf(getResourceBundle().getString("maxIdle")));
            jedisPoolConfig.setMinIdle(Integer.valueOf(getResourceBundle().getString("minIdle")));
        }
        return jedisPoolConfig;
    }

    /**
     * 获取本地redis.properties
     *
     * @return ResourceBundle
     */
    private static ResourceBundle getLocalResourceBundle() {
        bundle = ResourceBundle.getBundle(localBundleLocal);
        return bundle;
    }

    /**
     * 获取系统redis.properties
     *
     * @return ResourceBundle
     */
    private static ResourceBundle getSystemResourceBundle() {
        bundle = ResourceBundle.getBundle(systemBundleLocal);
        return bundle;
    }

    /**
     * 获取 ResourceBundle
     *
     * @return ResourceBundle
     */
    private static ResourceBundle getResourceBundle() {
        try {
            bundle = getSystemResourceBundle();
            if (null == bundle) {
                bundle = getLocalResourceBundle();
            }
        } catch (Exception e) {
            bundle = getLocalResourceBundle();
        }
        if (null == bundle) {
            throw new IllegalArgumentException("['redis.properties'] is not found!");
        }
        return bundle;
    }

    public static Integer getTimeout() {
        return Integer.valueOf(getResourceBundle().getString("timeout"));
    }

    private static void setTimeout(Integer timeout) {
        RedisConfigHelper.timeout = timeout;
    }

    public static String getPassword() {
        try{
            return getResourceBundle().getString("password");
        }catch (Exception e){
            return null;
        }
    }

    private static void setPassword(String password) {
        RedisConfigHelper.password = password;
    }

    public static Integer getPort() {
        return Integer.valueOf(getResourceBundle().getString("port"));
    }

    private static void setPort(Integer port) {
        RedisConfigHelper.port = port;
    }

    public static String getHost() {
        return getResourceBundle().getString("host");
    }

    private static void setHost(String host) {
        RedisConfigHelper.host = host;
    }
}
